package org.agony2d.display.core.supportClasses {
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import org.agony2d.core.agony_internal;
	
	use namespace agony_internal;
	
	/** 改变轴心数据时，坐标位置随之变化，使全局坐标不产生变化... */
public class PivotComp extends RecyclableComp {
  
    public function PivotComp() {
		m_pivotX = m_pivotY = 0
    }
	
	agony_internal function set pivotX( v:Number ) : void {
		this.setPivot(v, m_pivotY)
	}
	
	agony_internal function set pivotY( v:Number ) : void {
		this.setPivot(m_pivotX, v)
	}
	
	/** 坐标使用实际数据... */
	agony_internal function setPivot( pivotX:Number, pivotY:Number, global:Boolean = false ) : void {
		var point:Point
		
		g_cachedPoint.setTo(pivotX, pivotY)
		if (global) {
			PA = m_child.globalToLocal(g_cachedPoint)
			m_pivotX = point.x
			m_pivotY = point.y
			m_child.x = -point.x
			m_child.y = -point.y
		}
		else {
			g_cachedPoint = m_child.localToGlobal(g_cachedPoint)
			m_pivotX = pivotX
			m_pivotY = pivotY
			m_child.x = -pivotX
			m_child.y = -pivotY
		}
		point = this.parent ? this.parent.globalToLocal(g_cachedPoint) : g_cachedPoint
		this.x = point.x 
		this.y = point.y
	}
	
	agony_internal static function NewPivotComp( child:DisplayObject ) : PivotComp {
		var origin:PivotComp
		
		origin = (cachedPivotCompLength > 0 ? cachedPivotCompLength-- : 0) ? cachedPivotCompList.pop() : new PivotComp
		origin.m_child = child
		origin.addChild(child)
		return origin
	}
	
	agony_internal function reset() : void {
		super.reset()
		m_pivotX = m_pivotY = 0
	}
	
	
	agony_internal static var cachedPivotCompList:Array = []
	agony_internal static var cachedPivotCompLength:int
	
	agony_internal var m_pivotX:Number
	agony_internal var m_pivotY:Number
	agony_internal var m_child:DisplayObject
	
	
	override agony_internal function recycle() : void {
		cachedPivotCompList[cachedPivotCompLength++] = this
	}
	
	override agony_internal function dispose() : void {
		super.dispose()
		m_child = null
	}
	
}
}